﻿using System;
using System.Collections;
using System.Collections.Generic;
using com.guojin.mj.net.message.game;
using UnityEngine;
using  UnityEngine.UI;

public class RoomRulePage : EventManager
{
    private static RoomRulePage instance;

    public static RoomRulePage GetInstance()
    {
        return instance;
    }

    void Awake()
    {
        instance = this;
    }


    public override void InformationSetting()
    {
        AddListenerToButton(closeBtn, CloseBtnClick);
        AddListenerToButton(OnceChapter, OnceChapterBtnClick);
        AddListenerToButton(CardTypeTip, CardTypeBtnClick);
        SetText(roomIDTex, string.Format("【{0}】", GameRoomModel.GetInstance().RoomID));
        MessageManager.GetInstance().SendMsg1245(int.Parse( GameData.GetInstance().userID));
    }

    public GameObject OnceChapterObj;
    public GameObject CardTypeTipImgObj;
    public GameObject OnceChapterGroup;
    public Text roomIDTex;
    public Text chapterNumTex;
    public Text BigCardType;
    public Button OnceChapter;
    public Button CardTypeTip;
    public Button closeBtn;

    public void Init(OnceBeforeRet ob)
    {
        if (ob.chapterNum != 0)
        {
            SetText(chapterNumTex, string.Format("第{0}手回顾", ob.chapterNum));
        }

        if (string.IsNullOrEmpty(ob.bigCardType))
        {
            SetText(BigCardType, ob.bigCardType);
        }

        TooL.DestroyAllChildren(OnceChapterGroup);

        if (ob.list != null)
        {
            for (int i = 0; i <ob.list.Count ; i++)
            {
                var temp = TooL.LoadPrefab(OnceChapterGroup, "OnceChapterItem");
                if (ob.list[i].userID.ToString() == GameData.GetInstance().userID)
                {
                    SetTextSelect(temp.transform.FindChild("name").GetComponent<Text>());
                    SetTextSelect(temp.transform.FindChild("settleScore").GetComponent<Text>());
                }

                DownloadHeadTexture(temp.transform.FindChild("playerHead/head/mask/headImg").gameObject.GetComponent<RawImage>(), ob.list[i].avatar);

                if (ob.list[i].isZhuang)
                {
                    temp.transform.FindChild("playerHead/zhuang").gameObject.SetActive(true);
                }

                temp.transform.FindChild("cardType").GetComponent<Image>().sprite = Resources.Load<Sprite>("CardType/" + ob.list[i].cardType);

                if (ob.list[i].cards != null)
                {
                    for (int j = 0; j < ob.list[i].cards.Length; j++)
                    {
                        temp.transform.FindChild("cardGroup").GetChild(j).GetComponent<Image>().sprite =
                            Resources.Load<Sprite>("Card/"+ob.list[i].cards[j].ToString());
                        temp.transform.FindChild("cardGroup").GetChild(j).gameObject.SetActive(true);
                    }
                }

                SetText(temp.transform.FindChild("name").GetComponent<Text>(), ob.list[i].name);

                if (ob.list[i].settleScore > 0)
                {
                    SetText(temp.transform.FindChild("settleScore").GetComponent<Text>(),
                        "+" + ob.list[i].settleScore.ToString());
                }
                else
                {
                    SetText(temp.transform.FindChild("settleScore").GetComponent<Text>(),
                       ob.list[i].settleScore.ToString());
                }
            }
        }
       
    }

    private void OnceChapterBtnClick()
    {
        OnceChapterObj.SetActive(true);
        CardTypeTipImgObj.SetActive(false);
    }

    private void CardTypeBtnClick()
    {
        CardTypeTipImgObj.SetActive(true);
        OnceChapterObj.SetActive(false);
    }

}
